﻿using System.Collections.Generic;
using System.Linq;
using KLS.PPDS.Core.Entity;
using KLS.PPDS.Data.Database.Repository.EntityRepository;

namespace KLS.PPDS.ApplicationService.Manager
{
    public class PublicationManager : RepositoryManager<PublicationRepository>
    {
        public static IList<int> GetPublicationDistinctYear(int companyId)
        {
            if (companyId == 0) 
            {
                return Repository.FindAll().Where(c=>c.IsDeleted.Equals(false)).Select(c => c.Year).Distinct().ToList();
            }
            else if (companyId > 0) 
            {
            }
            return Repository.FindAll().Where(c => c.IsDeleted.Equals(false) && c.CompanyId == companyId).Select(c => c.Year).Distinct().ToList();
        }

        public static void Save(Publication publication)
        {
            publication.InitAudit();
            if (publication.Id>0)
            {
                Repository.Update(publication);
                LogManager.LogDbUpdate(publication);
            }
            else
            {
                Repository.SaveAndEvict(publication);
                LogManager.LogDbAdd(publication);                
            }
            
        }



        public static IEnumerable<Publication> GetAllPublication(int companyId)
        {            
            if (companyId > 0) 
            {
                return Repository.FindAll().Where(c => c.IsDeleted == false && c.CompanyId == companyId).OrderBy(c => c.PublicationName).AsEnumerable();
            }
            return Repository.FindAll().Where(c=>c.IsDeleted==false).OrderBy(c=>c.PublicationName).AsEnumerable();
        }

        public static IEnumerable<Publication> GetAllPublication(int customerId, int year, int month)
        {
           var pubs = Repository.GetPublication(customerId, year, month);
            
            foreach (var publication in pubs)
            {
                publication.MonthName = YearMonthManager.GetShortMonthName(publication.Month);
            }
            
            return pubs;
        }

        public static IEnumerable<Publication> GetPublicationForTaskAssigned(int useId, int customerId, int year, int month) 
        {
           // var pubs = new List<Publication>();
            
            var company = CompanyManager.GetAllCompanyGraphicsUserId(useId).Select(c=>c.Id).ToList();

           var pubs = Repository.GetPublicationForTaskAssigned(company, customerId, year, month);

            foreach (var publication in pubs)
            {
                publication.MonthName = YearMonthManager.GetShortMonthName(publication.Month);
            }

            return pubs;

        }

        public static Publication GetPublicationById(int Id)
        {
            return Repository.FindOne(Id);
        }

        public static int GetPageCountByPublicationId(int Id) 
        {
            var pub = GetPublicationById(Id);

            if (pub != null && pub.Pages.HasValue) 
            {
                return pub.Pages.Value;
            }

            return 0;
        }

        public static void Delete(int Id)
        {
            var pub = Repository.FindOne(Id);
            if (pub!=null)
            {
                pub.IsDeleted = true;
                Repository.Update(pub);
                LogManager.LogDbDelete(pub);
            }
            
        }
    }
}
